import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-02-26
 * Time: 12:08
 */
public class ReflectClassDemo {

    //通过反射去创建对象
    public static void reflectNewInstance() {
        try {
            //1.获取class对象
            Class<?> c1 = Class.forName("Student");
            //2.创建类的实例
            Student student = (Student) c1.newInstance();
            System.out.println("学生对象："+student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


    public static void reflectPrivateConstructor() {
        try {
            //1. 获取class对象
            Class<?> c1 = Class.forName("Student");
            //2. 获取构造方法（私有）
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);
            //3. 程序员确定要调用
            constructor.setAccessible(true);
            //4. 调用
            Student student =  (Student) constructor.newInstance("张三",21);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void reflectPrivateField() {
        try {
            //1. 获取class对象
            Class<?> c1 = Class.forName("Student");
            //2. 获取字段(私有)
            Field field = c1.getDeclaredField("name");
            //3. 程序员确定修改
            field.setAccessible(true);
            //4. 修改
            Student student = (Student) c1.newInstance();
            field.set(student,"米老鼠");
            System.out.println(student);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateMethod() {
        try {
            //1. 获取class对象
            Class<?> c1 = Class.forName("Student");
            //2. 获取方法(私有)
            Method method = c1.getDeclaredMethod("function", String.class);
            //3. 程序员确定修改
            method.setAccessible(true);
            //4.方法调用
            Student student = (Student) c1.newInstance();
            method.invoke(student,"我是一个参数！");


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
}
